#include <windows.h>
#include "lua.h"
#include "lstate.h"
#include "luauser.h"
#include <stdlib.h>

void LuaLockInitial(lua_State * L)
{
	if (!L->l_G->m_cs)
	{
		/* Create a mutex */
		L->l_G->m_cs = malloc(sizeof(CRITICAL_SECTION));
		InitializeCriticalSection(L->l_G->m_cs);
	}
}

void LuaLockFinal(lua_State * L) /* Not called by Lua. */
{
	/* Destroy a mutex. */
	if (L->l_G->m_cs)
	{
		DeleteCriticalSection(L->l_G->m_cs);
		free(L->l_G->m_cs);
		L->l_G->m_cs = NULL;
	}
}

void LuaLock(lua_State * L)
{
	/* Wait for control of mutex */
	EnterCriticalSection(L->l_G->m_cs);
}

void LuaUnlock(lua_State * L)
{
	/* Release control of mutex */
	LeaveCriticalSection(L->l_G->m_cs);
}